<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
      text-decoration: none;
      vertical-align: middle;
      color: white;
      font-family: '微软雅黑';
      font-size:14px;
    }
    .box{
      width: 590px;
      height: 470px;
      border:1px solid #ccc;
      position: relative;
      left:50%;
      transform: translateX(-50%);
      top:20px;
      overflow: hidden;
    }
    .box ul{
      width: 3000px;
      position: absolute;
      left: 0;
      top: 0;
    }
    li{
      float: left;
    }
    .btns{
      position: absolute;
      top:50%;
      transform: translateY(-50%);
      height: 60px;
      width: 100%;
    }
    .btns a{
      display: block;
      width: 40px;
      height: 60px;
      background-color: rgba(0,0,0,0.5);
      float: left;
      line-height: 60px;
      text-align: center;
    }
    .btns a:last-child{
      float: right;
    }
    .dot{
      position: absolute;
      bottom: 20px;
      left:20px;
    }
    .dot span{
      width: 14px;
      height: 14px;
      border-radius: 50%;
      border: 1px solid #ccc;
      display: inline-block;
      margin-right: 10px;
      cursor: pointer;
    }
    .active{
      background-color: white;
      box-shadow:0 0 5px 5px rgba(255,255,255,0.8);
    }
  </style>
</head>
<body>
  <div class="box">
    <ul>
      <li><a href="http://www.jd.com"><img src="img/1.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/2.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/3.jpg" alt=""></a></li>
      <li><a href="http://www.jd.com"><img src="img/4.jpg" alt=""></a></li>
    </ul>
    <div class="btns">
      <a href="javascript:;">&lt;</a>
      <a href="javascript:;">&gt;</a>
    </div>
    <div class="dot">
      <!-- <span class="active"></span>
      <span></span>
      <span></span>
      <span></span> -->
      <!-- 动态创建 -->
    </div>
  </div>
  <script>
    var dot = document.querySelector('.dot');
    var imgs = document.querySelectorAll('img');
    console.log(imgs);
    //动态创建小圆点
    for(var i = 0; i < imgs.length; i++){
      var span = document.createElement('span');
      dot.appendChild(span);
      i == 0 ? span.className = 'active' : span.className ='';
      //给span小圆点注册点击事件
      span.onclick = span_click;
      span.myindex = i;
      clear(span);
    }
    function span_click(){
      pageindex = this.myindex;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
      }
        dot.children[pageindex].className = 'active';
    }
    
    //给右侧按钮注册点击事件
    var rbtn = document.querySelector('.btns a:last-child');
    var ul = document.querySelector('.box ul');
    console.log(ul)
    var pageindex = 0;
    rbtn.onclick = function(){
      if(pageindex == imgs.length){
        ul.style.left = 0 + 'px';
        pageindex = 0;
      }
      pageindex ++; 
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      //设置小圆点随着图片变化 自身加active类名
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
       if(pageindex >= imgs.length){
        dot.children[0].className = 'active';
       }else{
        dot.children[pageindex].className = 'active';
       }
      }
    }
    clear(rbtn);
    //克隆第一张图片放到末尾
    var li = ul.children[0];
    var clone_li = li.cloneNode(true);
    ul.appendChild(clone_li)
    console.log(clone_li);

    //给左侧按钮注册点击事件
    var lbtn = document.querySelector('.btns a:first-child');
    console.log(lbtn);
    lbtn.onclick = function(){
      //点击向左移动的时候将 pageindex设置为最后一张图片的索引
      if(pageindex == 0){
        pageindex = imgs.length;
        ul.style.left = -imgs[0].offsetWidth*pageindex + 'px';
      }
      console.log(ul.style.left)
      pageindex --;
      animate(ul,ul.offsetLeft,-imgs[0].offsetWidth*pageindex,50);
      //设置图片移动 小圆点也移动到对应的图片的索引
      for(var i = 0; i < imgs.length; i++){
        dot.children[i].className = '';
      }
      dot.children[pageindex].className = 'active';
    }
    clear(lbtn);
    
    //给rbtn元素注册自动播放
    var id = setInterval(function(){
      rbtn.click();
    }, 1000);
    //封装清除自动播放的定时器
    function clear(btn){
      btn.onmouseover = function(){
        window.clearInterval(id)
      }
      btn.onmouseout = function(){
        id = setInterval(function(){
          rbtn.click()
        }, 1000);
      } 
    }
    //动画
    function animate(element,current,target,step){
      if(element.timeID){
        clearInterval(element.timeID);
        element.timeID = null;
      }
      if(current > target){
        step = - Math.abs(step);
      }
      element.timeID = setInterval(function(){
        if(Math.abs(current - target) < Math.abs(step)){
          current = target;
          clearInterval(element.timeID);
        }else{
          current += step;
        }
        element.style.left = current + 'px';
      }, 30);
    }
  </script>
</body>
</html>